package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = sc.nextLine();
        System.out.println("请输入方法名:");
        String way = sc.nextLine();
        try {
            Class cls = Class.forName(className);
            Constructor cs = cls.getConstructor(String.class);
            Object obj = cs.newInstance("李浩");
            Method method = cls.getMethod(way);
            method.invoke(obj);

            Method method2 = cls.getMethod(way,String.class);
            method2.invoke(obj,"你好,你是谁");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
